const Base = require('./base.js');

module.exports = class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get()

    delete query.page
    delete query.size

    const model = this.model('banner');
    const data = await model.where(query).order(['id DESC']).where({type:1}).page(page, size).countSelect();

    return this.success(data);
  }

  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('banner');

    if (id > 0) {
      await model.where({id: id}).update(values);
    } else {
      delete values.id;
      const data = values
      data.type = 1
      await model.add(data);
    }
    return this.success(values);
  }

  async deleteAction() {
    const id = this.get('id');
    await this.model('banner').where({id: id}).limit(1).delete();

    return this.success();
  }

  async actListAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get()

    delete query.page
    delete query.size

    const model = this.model('banner');
    const data = await model.where(query).order(['id DESC']).where({type:2}).page(page, size).countSelect();

    return this.success(data);
  }

  async actPutAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('banner');

    if (id > 0) {
      await model.where({id: id}).update(values);
    } else {
      delete values.id;
      const data = values
      data.type = 2
      await model.add(data);
    }
    return this.success(values);
  }

  async actDeleteAction() {
    const id = this.get('id');
    await this.model('banner').where({id: id}).limit(1).delete();

    return this.success();
  }
};
